package com.testdemo.web.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.labmanager.JsonModule.ResponseBase;
import com.testdemo.web.service.GroupService;
import com.testdemo.web.service.LabService;
import com.testdemo.web.service.MyUserService;
import com.testdemo.web.vo.Group;
import com.testdemo.web.vo.Session;
import com.testdemo.web.vo.SessionManager;
import com.testdemo.web.vo.UserInfo;


@Controller
@RequestMapping("/group")
public class GroupController {
	private SessionManager sessionManager;
	public GroupController() {
		sessionManager = AccountController.sessionManager;
	}
	
	@Resource
	private GroupService groupService;
	@Resource
	private LabService labService;
	@Resource
	private MyUserService myUserService;
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase AddGroup(@RequestBody Group group) {
		ResponseBase responseBase = new ResponseBase();
		//检验数据
		if(group.checkData() == false) {
			responseBase.setError(-5, "请求数据有误");
			return responseBase;
		}
		//登陆检验
		Session session = sessionManager.selectSessionByAccessToken(group.getSession());
		if(session == null) {
			responseBase.setError(-1, "你还没有登陆，请先登陆");
			return responseBase;
		}
		//权限检验
		UserInfo userInfo = myUserService.selectUserInfoByName(session.getUsername());
		if(!labService.checkuserName(userInfo.getUsername(), userInfo.getLabid())) {
			responseBase.setError(-1, "你没有权限创建小组");
			return responseBase;
		}
		group.setLabid(userInfo.getLabid());
		groupService.insertGroup(group);
		
		return responseBase;
	}
	
	@RequestMapping(value = "/delete",method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase DeleteGroup(@RequestBody Group group) {
		ResponseBase responseBase = new ResponseBase();
		
		if(!group.checkData()) {
			responseBase.setError(-5, "缺少必须参数");
			return responseBase;
		}
		//登陆检验
				Session session = sessionManager.selectSessionByAccessToken(group.getSession());
				if(session == null) {
					responseBase.setError(-1, "你还没有登陆，请先登陆");
					return responseBase;
				}
				//权限检验
				UserInfo userInfo = myUserService.selectUserInfoByName(session.getUsername());
				if(!labService.checkuserName(userInfo.getUsername(), userInfo.getLabid())) {
					responseBase.setError(-1, "你没有权限删除小组");
					return responseBase;
				}
				//对于实验室检验
				group = groupService.selectGroupByGroupId(group.getGroupid());
				if(!group.getLabid().equals(userInfo.getLabid())) {
					responseBase.setError(-1, "你没有权限删除小组");
					return responseBase;
				}
				
				groupService.deleteGroup(group.getGroupid());
		
		return responseBase;
	}
	
	@RequestMapping(value = "/update",method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase UpdateGroup(@RequestBody Group group) {
		ResponseBase responseBase = new ResponseBase();
		if(!group.checkData()) {
			responseBase.setError(-5, "缺少必须参数");
			return responseBase;
		}
		//登陆检验
				Session session = sessionManager.selectSessionByAccessToken(group.getSession());
				if(session == null) {
					responseBase.setError(-1, "你还没有登陆，请先登陆");
					return responseBase;
				}
				//权限检验
				UserInfo userInfo = myUserService.selectUserInfoByName(session.getUsername());
				if(!labService.checkuserName(userInfo.getUsername(), userInfo.getLabid())) {
					responseBase.setError(-1, "你没有权限修改小组");
					return responseBase;
				}
				//对于实验室检验
				Group orginGroup = groupService.selectGroupByGroupId(group.getGroupid());
				if(!orginGroup.getLabid().equals(userInfo.getLabid())) {
					responseBase.setError(-1, "你没有权限修改小组");
					return responseBase;
				}
				if(group.getUsername() == null) {
					group.setUsername(orginGroup.getUsername());
				}
				groupService.updateGroup(group);
		
		return responseBase;
	}
	@RequestMapping(value = "/get",method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase GetGroupList(@RequestBody UserInfo userInfo) {
		ResponseBase responseBase = new ResponseBase();
		if(!userInfo.checkData()) {
			responseBase.setError(-5, "缺少必须参数");
			return responseBase;
		}
		Session session = sessionManager.selectSessionByAccessToken(userInfo.getSession());
		if(session == null) {
			responseBase.setError(-1, "你还没有登陆，请先登陆");
			return responseBase;
		}
		//权限检验
		userInfo = myUserService.selectUserInfoByName(session.getUsername());
//		if(!labService.checkuserName(userInfo.getUsername(), userInfo.getLabid())) {
//			responseBase.setError(-1, "你没有权限获取小组");
//			return responseBase;
//		}
		List<Group> list = groupService.selectGroupByLabId(userInfo.getLabid());
		responseBase.setData(list);
		return responseBase;
	}
}
